﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

#endregion

using Microsoft.TeamFoundation.WorkItemTracking.Client;
using ScrumTable.Common;
using ScrumTable.DL.Data.Schema;

namespace ScrumTable.DL.Data.TFS.Services.TeamFoundation
{
    /// <summary>
    /// This class provides additional functionalities for work item
    /// handling.
    /// </summary>
    public static class TfsWorkItem
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns the default (initial) id of a WorkItem.
        /// </summary>
        public const int DefaultId = 0;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the field information based on the given schema entry instance.
        /// </summary>
        /// <param name="input">Specifies the work item pointer, must not be null.</param>
        /// <param name="toGetValue">Specifies the schema with the element information to retrieve.</param>
        /// <returns>Returns the retrieved field or a null reference if it could not be found.</returns>
        public static Field TryGetField(this WorkItem input, SchemaBase toGetValue)
        {
            return TryGetField(input, toGetValue.SchemaMappedName);
        }

        /// <summary>
        /// Gets the field information based on the given schema entry instance.
        /// </summary>
        /// <param name="input">Specifies the work item pointer, must not be null.</param>
        /// <param name="toGetValue">Specifies the name of the field to retrieve.</param>
        /// <returns>Returns the retrieved field or a null reference if it could not be found.</returns>
        public static Field TryGetField(this WorkItem input, string toGetValue)
        {
            PreCondition.AssertNotNull(input, "input");

            if (toGetValue != null)
            {
                if (input.Fields.Contains(toGetValue))
                {
                    return input.Fields[toGetValue];
                }
            }
            return null;
        }

        /// <summary>
        /// Gets the field value based on the given schema entry instance.
        /// </summary>
        /// <typeparam name="T">Specifies the type of the value to retrieve.</typeparam>
        /// <param name="input">Specifies the work item pointer, must not be null.</param>
        /// <param name="toGetValue">Specifies the schema with the element information to retrieve.</param>
        /// <param name="defaultValue">Specifies the fallback field value to return if the field could not be found.</param>
        /// <returns>Returns the retrieved field value or the given default value if the field could not be found.</returns>
        public static T TryGetValue<T>(this WorkItem input, SchemaBase toGetValue, T defaultValue)
        {
            return TryGetValue(input, toGetValue.SchemaMappedName, defaultValue);
        }

        /// <summary>
        /// Gets the field value based on the given schema entry instance.
        /// </summary>
        /// <typeparam name="T">Specifies the type of the value to retrieve.</typeparam>
        /// <param name="input">Specifies the work item pointer, must not be null.</param>
        /// <param name="toGetValue">Specifies the name of the field to retrieve.</param>
        /// <param name="defaultValue">Specifies the fallback field value to return if the field could not be found.</param>
        /// <returns>Returns the retrieved field value or the given default value if the field could not be found.</returns>
        public static T TryGetValue<T>(this WorkItem input, string toGetValue, T defaultValue)
        {
            PreCondition.AssertNotNull(input, "input");
            
            if (toGetValue != null)
            {
                return input.TryGetValue(input.TryGetField(toGetValue), defaultValue);
            }
            return defaultValue;
        }

        /// <summary>
        /// Sets the field value based on the given schema entry instance.
        /// </summary>
        /// <param name="input">Specifies the work item pointer, must not be null.</param>
        /// <param name="toSetValue">Specifies the schema with the element information to retrieve.</param>
        /// <param name="fieldValue">Specifies the value of the field.</param>
        public static void TrySetValue(this WorkItem input, SchemaBase toSetValue, object fieldValue)
        {
            PreCondition.AssertNotNull(input, "input");
            PreCondition.AssertNotNull(toSetValue, "toSetValue");

            Field tfsField = input.TryGetField(toSetValue);

            if (tfsField != null && !tfsField.IsReadOnly())
            {
                // the TFS WorkItem must contain a field with the given name
                tfsField.Value = fieldValue;    
            }
        }

        private static T TryGetValue<T>(this WorkItem input, Field field, T defaultValue)
        {
            PreCondition.AssertNotNull(input, "input");

            if (field != null)
            {
                object retrievedValue = field.Value;

                if (retrievedValue is T)
                {
                    return (T)retrievedValue;
                }
            }
            return defaultValue;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}